import { interval } from '@/ayaka'
import TransportTaskManager from '@/room/task/base/transport'
import { OneToOneTransportData } from '@/room/task/base/transport/types'

/**
 * Nuker 控制器
 */
export default class NukerController {
    /**
     * 搬运 Nuker 资源任务名字
     */
    public static readonly TRANSPORT_NUKER_TASK_NAME = '搬运核弹的资源'

    /**
     * 不可实例化
     */
    protected constructor() {
        throw new Error('NukerController 不可被实例化！')
    }

    /**
     * 控制器工作
     */
    @interval(631)
    public static run(room: MyRoom) {
        if (!room.memory.structures.nuker || room.controller.level < 8 || !room.storage) return

        const nuker = room.nuker
        if (!nuker?.my) return

        if (TransportTaskManager.searchTransportTaskByName(room, NukerController.TRANSPORT_NUKER_TASK_NAME).length) {
            return
        }

        const needEnergy = Math.min(nuker.store.getFreeCapacity(RESOURCE_ENERGY), 30000)
        const needG = Math.min(nuker.store.getFreeCapacity(RESOURCE_GHODIUM), 5000)

        if (needEnergy <= 0 && needG <= 0) return

        if (room.storage.store[RESOURCE_ENERGY] < 70000 || room.storage.store[RESOURCE_GHODIUM] < needG) {
            return
        }

        const data: OneToOneTransportData = {
            sourceId: room.storage.id,
            targetId: nuker.id,
            resInfo: {
                [RESOURCE_ENERGY]: {
                    require: needEnergy,
                    has: 0,
                },
                [RESOURCE_GHODIUM]: {
                    require: needG,
                    has: 0,
                },
            },
        }

        TransportTaskManager.addOneToOneTransportTask(room, NukerController.TRANSPORT_NUKER_TASK_NAME, data)
    }
}
